<template>
    <baseModel ref="base" spellcheck="false" @dragstart.prevent>
        <router-view v-slot="{ Component }">
            <transition :name="`${index ? '' : 'animate__animated '}`"
                        enter-active-class="animate__fadeInLeft animate__fast"
                        leave-active-class="animate__fadeOutRight animate__faster" mode="out-in">
                <component :is="Component"/>
            </transition>
        </router-view>
    </baseModel>
</template>

<script setup>
//body添加el-popup-parent--hidden,避免windows下dialog出现body偏移
import router from "@/router"
import {preventDefaultTouch} from "@usehook/useTouch"
import FingerprintJS from "@fingerprintjs/fingerprintjs"
import iconfont from "@/assets/icon/iconfont.woff2?t=1689449444"
import {loadFont} from "@/utils/tools"
import {useRequest} from "@usehook"
import {useUserStore, storeToRefs} from "@/pinia"
import {apiPrefix, consolePageUrl} from "@/utils/constant"

// 初始化FingerprintJS实例
FingerprintJS.load().then((fp) => {
    // 获取访客标识符（visitor ID）
    fp.get().then((result) => {
        console.log(result.visitorId);
    })
})

const forbidRightClick = () => {
    document.oncontextmenu = () => {
        return false
    }
}

const forbidDragstart = () => {
    document.ondragstart = () => {
        return false
    }
}

let index = true
document.body.classList.add('el-popup-parent--hidden')

watch(() => router.currentRoute.value.path, (value) => {
    index = value === "/"
})

// 从控制台跳转登录
const userStore = useUserStore()
const {getUserInfo} = storeToRefs(userStore)

if (window.opener) {
    window.addEventListener("message", (e) => {
        if (e.origin === consolePageUrl && e.data.startsWith("token@") && !getUserInfo.value.sequence) {
            useRequest(apiPrefix + "/exchangeToken", {eToken: e.data.slice(6)}).then(resp => {
                if (resp && resp.status) {
                    userStore.login(resp.data)
                }
            })
        }
    })
    // 发送消息给主窗口
    window.opener.postMessage("load success", "*")
}

onMounted(() => {
    forbidRightClick()
    forbidDragstart()
    preventDefaultTouch()
    document.addEventListener("touchend", () => true)
    loadFont(iconfont)
})
</script>